package com.ruoyi.mushcoin.controller;

import java.util.ArrayList;
import java.util.List;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.excel.core.ExcelResult;
import com.ruoyi.common.core.core.domain.R;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.web.annotation.RepeatSubmit;
import com.ruoyi.common.web.core.BaseController;
import jakarta.annotation.Resource;
import com.ruoyi.mushcoin.domain.vo.TgUserGameCurrencyVo;
import com.ruoyi.mushcoin.domain.vo.TgUserGameCurrencyImportVo;
import com.ruoyi.mushcoin.domain.bo.TgUserGameCurrencyBo;
import com.ruoyi.mushcoin.listener.TgUserGameCurrencyImportListener;
import com.ruoyi.mushcoin.service.ITgUserGameCurrencyService;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.common.orm.core.page.TableDataInfo;

/**
 * TG用户游戏币Controller
 *
 * @author billy
 * 2024-11-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mushcoin/userGameCurrency")
public class TgUserGameCurrencyController extends BaseController
{
    @Resource
    private ITgUserGameCurrencyService tgUserGameCurrencyService;

    /**
     * 查询TG用户游戏币列表
     */
    @SaCheckPermission("mushcoin:userGameCurrency:list")
    @GetMapping("/list")
    public TableDataInfo<TgUserGameCurrencyVo> list(TgUserGameCurrencyBo tgUserGameCurrencyBo)
    {
        return tgUserGameCurrencyService.selectPage(tgUserGameCurrencyBo);
    }

    /**
     * 导出TG用户游戏币列表
     */
    @SaCheckPermission("mushcoin:userGameCurrency:export")
    @Log(title = "TG用户游戏币", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TgUserGameCurrencyBo tgUserGameCurrencyBo)
    {
        List<TgUserGameCurrencyVo> list = tgUserGameCurrencyService.selectList(tgUserGameCurrencyBo);
        ExcelUtil.exportExcel(list, "TG用户游戏币", TgUserGameCurrencyVo.class, response);
    }

    /**
     * 导入数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "TG用户游戏币", businessType = BusinessType.IMPORT)
    @SaCheckPermission("mushcoin:userGameCurrency:import")
    @PostMapping("/importData")
    public R<Void> importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelResult<TgUserGameCurrencyImportVo> result = ExcelUtil.importExcel(file.getInputStream(), TgUserGameCurrencyImportVo.class, new TgUserGameCurrencyImportListener(updateSupport));
        return R.ok(result.getAnalysis());
    }

    @SaCheckPermission("mushcoin:userGameCurrency:import")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "TG用户游戏币", TgUserGameCurrencyImportVo.class, response);
    }

    /**
     * 获取TG用户游戏币详细信息
     */
    @SaCheckPermission("mushcoin:userGameCurrency:query")
    @GetMapping(value = "/{id}")
    public R<TgUserGameCurrencyVo> getInfo(@PathVariable Long id)
    {
        return R.ok(tgUserGameCurrencyService.selectById(id));
    }

    /**
     * 新增TG用户游戏币
     */
    @SaCheckPermission("mushcoin:userGameCurrency:add")
    @Log(title = "TG用户游戏币", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public R<Void> add(@Validated @RequestBody TgUserGameCurrencyBo tgUserGameCurrencyBo)
    {
        boolean inserted = tgUserGameCurrencyService.insert(tgUserGameCurrencyBo);
        if (!inserted) {
            return R.fail("新增TG用户游戏币记录失败！");
        }
        return R.ok();
    }

    /**
     * 修改TG用户游戏币
     */
    @SaCheckPermission("mushcoin:userGameCurrency:edit")
    @Log(title = "TG用户游戏币", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public R<Void> edit(@Validated @RequestBody TgUserGameCurrencyBo tgUserGameCurrencyBo)
    {
        Boolean updated = tgUserGameCurrencyService.update(tgUserGameCurrencyBo);
        if (!updated) {
            return R.fail("修改TG用户游戏币记录失败!");
        }
        return R.ok();
    }

    /**
     * 删除TG用户游戏币
     */
    @SaCheckPermission("mushcoin:userGameCurrency:remove")
    @Log(title = "TG用户游戏币", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable Long[] ids)
    {
        boolean deleted = tgUserGameCurrencyService.deleteByIds(ids);
        if (!deleted) {
            return R.fail("删除TG用户游戏币记录失败!");
        }
        return R.ok();
    }
}
